# -*- coding:utf-8 -*-
import pathlib
import sqlite3
import os
import threading

ABSPATH = pathlib.Path(__file__).resolve().parent.parent


class DB:
    instance = None
    lock = threading.RLock()
    connection = sqlite3.connect(os.path.join(ABSPATH, "api.db"))

    def __new__(cls, *args, **kwargs):
        if cls.instance:
            return cls.instance

        with cls.lock:
            cls.instance = object.__new__(cls)
            return cls.instance

    @classmethod
    def session(cls):
        return cls.connection

    @classmethod
    def execute_all(cls, sql):
        c = cls.connection.cursor()
        c.execute(sql)
        results = c.fetchall()
        print(results)

    @classmethod
    def execute_one(cls, sql):
        c = cls.connection.cursor()
        c.execute(sql)
        result = c.fetchone()
        print(result)

    @classmethod
    def execute_commit(cls, sql, *args):
        c = cls.connection.cursor()
        c.execute(sql, *args)
        cls.connection.commit()


if __name__ == '__main__':
    name = 6
    # DB.execute_commit("CREATE  TABLE  table_name(line_name text,class_name text,id integer)")
    DB.execute_commit("insert into table_name(id,line_name,class_name) values (3,3434,?)", name)
